package com.cmc.cloud.cmclink.doc.controller.admin;

import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.convert.VgmConvert;
import com.cmc.cloud.cmclink.doc.entity.VgmDO;
import com.cmc.cloud.cmclink.doc.service.VgmService;
import com.cmc.cloud.cmclink.doc.util.FileOperateUtil;
import com.cmc.cloud.cmclink.doc.vo.vgm.*;
import com.cmes.framework.biz.advice.annotations.UseResponsePropertyAdvice;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.operatelog.core.annotations.OperateLog;
import com.cmes.framework.operatelog.core.enums.OperateTypeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;


/**
 * VMG信息前端接口Controller
 *
 * @author codegen
 */
@Tag(name = "管理后台 - VMG信息")
@RestController
@Validated
@RequestMapping(UrlConstants.APPLICATION_URL + "/vgm")
@Slf4j
public class VgmController {

    @Resource
    private VgmService vgmService;

    @Operation(summary = "导入VMG信息")
    @PostMapping("/import")
    @PreAuthorize("@ss.hasPermission('doc:vgm:import')")
    @OperateLog(name = "导入VMG信息", type = OperateTypeEnum.CREATE)
    public CommonResult<List<VgmImportReqVO>> importVgm(@Valid @RequestBody VgmImportAllReqVO vgmImportAllReqVO) {
        return CommonResult.success(vgmService.importVgm(vgmImportAllReqVO.getVgmImportReqVoList(), vgmImportAllReqVO.getVesselCode(), vgmImportAllReqVO.getVoyage(), vgmImportAllReqVO.getCurrentPort()));
    }

    @Operation(summary = "创建VMG信息")
    @PostMapping("/create")
    @PreAuthorize("@ss.hasPermission('doc:vgm:create')")
    @OperateLog(name = "创建VMG信息", type = OperateTypeEnum.CREATE)
    public CommonResult<Void> createVgm(@Valid @RequestBody VgmCreateReqVO vgmCreateReqVo) {
        vgmService.createVgm(vgmCreateReqVo);
        return CommonResult.success();
    }

    @Operation(summary = "更新VMG信息")
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermission('doc:vgm:update')")
    @OperateLog(name = "更新VMG信息", type = OperateTypeEnum.UPDATE)
    public CommonResult<Void> updateVgm(@Valid @RequestBody VgmUpdateReqVO updateReqVO) {
        vgmService.updateVgm(updateReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "删除VMG信息")
    @PostMapping("/delete/{id}")
    @Parameter(name = "id", description = "编号", required = true)
    @OperateLog(name = "删除VMG信息", type = OperateTypeEnum.DELETE)
    @PreAuthorize("@ss.hasPermission('doc:vgm:delete')")
    public CommonResult<Void> deleteVgm(@PathVariable("id") Long id) {
        vgmService.deleteVgm(id);
        return CommonResult.success();
    }

    @Operation(summary = "校验VMG信息")
    @PostMapping("/check")
    @PreAuthorize("@ss.hasPermission('doc:vgm:check')")
    @OperateLog(name = "校验VMG信息", type = OperateTypeEnum.GET)
    public CommonResult<List<VgmImportReqVO>> checkVgm(@Valid @RequestBody VgmImportAllReqVO vgmImportAllReqVO) {
        return CommonResult.success(vgmService.check(vgmImportAllReqVO.getVgmImportReqVoList(), vgmImportAllReqVO.getVesselCode(), vgmImportAllReqVO.getVoyage(), vgmImportAllReqVO.getCurrentPort()));
    }

    @Operation(summary = "获得VMG信息分页")
    @PostMapping("/page")
    @PreAuthorize("@ss.hasPermission('doc:vgm:query')")
    @UseResponsePropertyAdvice
    @OperateLog(name = "获得VMG信息分页", type = OperateTypeEnum.GET)
    public CommonResult<PageResult<VgmRespVO>> getVgmPage(@Valid @RequestBody VgmPageReqVO pageVo) {
        PageResult<VgmDO> pageResult = vgmService.getVgmPage(pageVo);
        List<VgmRespVO> results = VgmConvert.INSTANCE.convertList(pageResult.getList());
        return CommonResult.success(new PageResult<>(results, pageResult.getTotal()));
    }

    @Operation(summary = "获得VMG信息")
    @PostMapping("/info")
    @UseResponsePropertyAdvice
    @OperateLog(name = "获得VMG信息", type = OperateTypeEnum.GET)
    public CommonResult<List<VgmQueryInfoRespVO>> getVgm(@Valid @RequestBody VgmQueryInfoVO reqVo) {
        return CommonResult.success(VgmConvert.INSTANCE.convertVgmQueryInfoRespVo(vgmService.getVgmInfo(reqVo)));
    }

    @PostMapping("/excel-template")
    @Operation(summary = "下载excel导入模板")
    @PreAuthorize("@ss.hasPermission('doc:vgm:download')")
    @OperateLog(name = "VGM模板下载", type = OperateTypeEnum.GET)
    public CommonResult<Void> downloadExcelTemplateImport(HttpServletResponse response) throws IOException {
        String fileName = "VGMFormat.xls";
        String templatePath = "templates/VGMFormat.xls";
        return FileOperateUtil.downloadExcelTemplate(response, fileName, templatePath);
    }
}
